import { followAPI, FollowParams } from '@/API/commonAPI';
import { Toast } from 'antd-mobile';
import { useRef, useState } from 'react';

const useFollow = () => {
  const message = useRef<string>('');
  const id = useRef<string>('');
  const [loading, setLoading] = useState<boolean>(false);
  const handleFollow = async (followParams: FollowParams, type: 0 | 1) => {
    try {
      setLoading(true);
      const { data } = await followAPI(followParams);
      id.current = data.id;
      if (followParams.type === 'doc') {
        message.current = `${type === 0 ? '取关医生成功' : '关注医生成功'}`;
      } else if (followParams.type === 'knowledge') {
        message.current = `${type === 0 ? '取关文章成功' : '关注文章成功'}`;
      } else if (followParams.type === 'disease') {
        message.current = `${type === 0 ? '取关疾病成功' : '关注疾病成功'}`;
      } else if (followParams.type === 'topic') {
        message.current = `${type === 0 ? '取关话题成功' : '关注话题成功'}`;
      }
    } catch (error: any) {
      Toast.show(error.message);
    } finally {
      setLoading(false);
    }
  };
  return {
    id,
    message,
    loading,
    handleFollow,
  };
};


export default useFollow;